﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Temp_20190707_AForm
{
    public partial class DockFloatForm : Form
    {
        public DockFloatForm()
        {
            ControlStyles ControlStylesUAORSD = ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.DoubleBuffer;
            SetStyle(ControlStylesUAORSD, true);
            InitializeComponent();

            m_Timer = new Timer();
            m_Timer.Interval = 100;
            m_Timer.Tick += Timer_Tick;
            m_Timer.Enabled = false;
        }

        public static void ShowControl(DockPanel dockPanel, DockTipForm form2, Control control)
        {
            DockFloatForm form = new DockFloatForm();
            form.Form2 = form2;
            form.Control = control;
            form.DockPanel = dockPanel;

            control.Dock = DockStyle.Fill;
            form.Controls.Add(control);
            form.Show(dockPanel.FindForm());
        }

        public DockTipForm Form2 { get; set; }
        public Control Control { get; set; }
        public DockPanel DockPanel { get; set; }


        protected override void WndProc(ref Message m)
        {
            const int WM_NCLBUTTONDOWN = 0xA1;
            const int WM_NCLBUTTONUP = 0x00A2;


            base.WndProc(ref m);

            if (m.Msg == WM_NCLBUTTONDOWN)      OnNCMouseDown();
            else if (m.Msg == WM_NCLBUTTONUP)   OnNCMouseUp();  //WM_NCLBUTTONUP 消息没用
        }



        private Timer m_Timer = null;
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (Control.MouseButtons == MouseButtons.None)
            {
                m_Timer.Enabled = false;
                OnNCMouseUp();
            }
        }
        protected void OnNCMouseDown()
        {
            //MessageBox.Show("OnMouseDown");
            m_Timer.Enabled = true;
        }
        protected void OnNCMouseUp()
        {
            //MessageBox.Show("OnMouseUp");

            DockPanel.DoDock(Control);
        }



    }
}
